package com.sun;


import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class FileInfor {
    DecimalFormat df = new DecimalFormat("0.##");
    @Test
    public void info(){
        File files = new File("D:\\中科软信息\\需求\\代理人积分系统\\两核知识\\核保基础知识");

        /*        System.out.println("文件名字" + file.getName());
        System.out.println("文件绝对路径" + file.getAbsolutePath());
        System.out.println("文件相对路径" + file.getPath());
        System.out.println("文件父目录" + file.getParent());
        System.out.println("文件大小(字节)" + df.format(new Double(file.length()) / 1024 / 1024));
        System.out.println("文件是否存在" + file.exists());
        System.out.println("是不是文件" + file.isFile());
        System.out.println("是不是目录" + file.isDirectory());*/
        String sql = "insert into salelstrainfile(fileId,fileName,fileRealName,makeDate,makeTime,fileType,\n" +
                "       fileSize,operator,comCode,visRole,fileUrl,head,channel,\n" +
                "       Type,description,videoImg,backUp1,backUp2,backUp3)\n" +
                "      values";
        String makeDate = "2021-11-19";
        String makeTime = "16:48:00";
        String fileType = "98";
        String operator = "0000056303";
        String comCode = "86,8669";
        String visRole = "1,2";
        String head = "1";
        String channel = "Y";
        String Type = "p";
        String description = null;
        String videoImg = null;
        String backUp1 = null;
        String backUp2 = "86";
        String backUp3 = null;
        List<Map> list = new ArrayList();
        File[] listFiles = files.listFiles();
        String flag = "";
        for (int i = 0; i < listFiles.length; i++) {
            File file = listFiles[i];
            String fileId = UUID.randomUUID().toString();
            String fileName = file.getName().substring(0,file.getName().length()-4) ;
            String fileRealName = file.getName();
            String fileSize = df.format(new Double(file.length() )/ 1024 / 1024) + "MB";
            String fileUrl = fileId + ".pdf";
            if ( i == 0){
                flag = "('";
            }else {
                flag = "'),('";
            }
            String value = flag + fileId + "','" + fileName +"','"+ fileRealName +"','"+ makeDate +"','"+ makeTime +"','"+ fileType +"','"+ fileSize +"','"+ operator +"','" +
                    comCode + "','" + visRole + "','" + fileUrl + "','" +head + "','"+channel+ "','"+Type+ "',"+description+ ","+videoImg+ ","+backUp1 +",'"+ backUp2 + "'," + backUp3;
            sql += value;
            Map map = new HashMap();
            map.put("fileName",fileName);
            map.put("fileId",fileId);
            map.put("value",value);
            list.add(map);
            copy(file,new File("D:\\中科软信息\\需求\\代理人积分系统\\两核知识处理结果\\银保\\核保基础知识", fileUrl));

        }
        for (Object o : list) {
            System.out.println(o);
        }
        System.out.println(sql);
    }
    public static void copy(File file,File fileUrl){
        try(BufferedInputStream bfs = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(fileUrl))){
            IOUtils.copy(bfs,bos);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Test
    public void djks(){
        File file = new File("D:\\中科软信息\\需求\\代理人积分系统\\处理后的文件夹\\7a26e67e-1fb7-484d-bdcd-6bc73f6cb4e2.pdf");
        long length = file.length();
        System.out.println(length);
        String format = df.format(new Double(file.length()) / 1024 / 1024);
        System.out.println(format);
    }
    @Test
    public void creatFile(){
        File file = new File("D:\\中科软信息\\自学\\io\\new");
        if (file.exists()){
            file.delete();
            System.out.println("文件删除成功");
        }else {
            try {
                if (!file.equals(".")){
                    file.mkdirs();
                    System.out.println("新建文件夹成功");
                }else {
                    file.createNewFile();
                    System.out.println("新建文件成功");
                }
            } catch (IOException e) {
                System.out.println("新建文件错误 错误原因" + e);
            }
        }

    }
    @Test
    public void InputStreamTest1(){
        File file = new File("D:\\中科软信息\\自学\\io\\new2.txt");
        FileInputStream is = null;
        int readData = 0;
        try {
            is = new FileInputStream(file);
            while ((readData = is.read()) != -1){
                System.out.println((char) readData);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    @Test
    public void InputStreamTest2(){
        File file = new File("D:\\中科软信息\\自学\\io\\new2.txt");
        FileInputStream is = null;
        int len = 0;
        byte[] buf = new byte[8];
        try {
            is = new FileInputStream(file);
            while ((len = is.read(buf)) != -1){
                System.out.print(new String(buf, 0,len));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    @Test
    public void OutPutStreamTest1(){
        File file = new File("D:\\中科软信息\\自学\\io\\new3.txt");
        File file1 = new File("D:\\中科软信息\\自学\\io\\new2.txt");
        int readLen = 0;
        byte[] buf = new byte[8];
        try(FileOutputStream os = new FileOutputStream(file,true);FileInputStream is = new FileInputStream(file1)) {
            while ((readLen = is.read(buf)) != -1){
                System.out.println();
                os.write(buf,0,readLen);
            }
            os.write("\n".getBytes());
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void FileCopy(){

    }
}
